package servlet.PropertyManagementModule.Announcement;

import bean.dto.AnnouncementPage;
import bean.dto.EquipmentPage;
import bean.pojo.Announcement;
import bean.pojo.ComplaintRecord;
import com.alibaba.fastjson2.JSONObject;
import commons.baseServlet.BaseServlet;
import commons.format.NumberFormat;
import commons.r.R;
import service.AnnouncementService;
import service.ComplaintRecordService;
import service.impl.AnnouncementServiceImpl;
import service.impl.ComplaintRecordServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

/**
 * servlet.PropertyManagementModule.Announcement
 * User: xlh
 * Date: 2024/12/23 14:18
 * motto:推到高塔
 * Description:
 * Version: V1.0
 */
@WebServlet(value = "/announcement/*")
public class AnnouncementServlet extends BaseServlet {
    public R findByPage(HttpServletRequest request){
        Integer page= NumberFormat.stringToInteger(request.getParameter("page"));
        Integer limit= NumberFormat.stringToInteger(request.getParameter("limit"));
        String selectMonth= request.getParameter("selectMonth");
        String announcementTitle= request.getParameter("announcementTitle");
        AnnouncementService announcementService=new AnnouncementServiceImpl();
        AnnouncementPage announcementPage=new AnnouncementPage();
        //查询与维修一样。
        announcementPage.setSelectMonth(selectMonth);
        announcementPage.setAnnouncementTitle(announcementTitle);
        announcementPage.setPage(page);
        announcementPage.setLimit(limit);

        List<Announcement> list = null;
        Long  total=announcementService.findByTotal(announcementPage);
        //当前页码的数据
        if(total>0) {
            //当前页码的数据
            list =announcementService.findByPage(announcementPage);
        }
        R r= R.ok().addData("total",total).addData("list",list);
        return r;
    }
    public R updatAnnouncement(HttpServletRequest request){
        try {
            BufferedReader bufferedReader=request.getReader();
            String data=bufferedReader.readLine();
            Announcement announcement= JSONObject.parseObject(data,Announcement.class);
            AnnouncementService announcementService=new AnnouncementServiceImpl();
            boolean t=announcementService.updatAnnouncement(announcement);
            return t?R.ok("修改成功"):R.ok("修改失败");
        } catch (
                IOException e) {
            e.printStackTrace();
        }
        return R.error("添加出错");
    }
    public R addAnnouncement(HttpServletRequest request){
        try {
            BufferedReader bufferedReader=request.getReader();
            String data=bufferedReader.readLine();
            Announcement announcement= JSONObject.parseObject(data,Announcement.class);
            AnnouncementService announcementService=new AnnouncementServiceImpl();
            boolean t=announcementService.addAnnouncement(announcement);
            return t?R.ok("添加成功"):R.ok("添加失败");
        } catch (
                IOException e) {
            e.printStackTrace();
        }
        return R.error("添加出错");
    }

    public R deleteAnnouncement(HttpServletRequest request){
        Integer announcementId= NumberFormat.stringToInteger(request.getParameter("announcementId"));
            AnnouncementService announcementService=new AnnouncementServiceImpl();
            boolean t=announcementService.deleteAnnouncement(announcementId);
            return t?R.ok("删除成功"):R.ok("删除失败");
    }
}
